//
// Created on 2022/11/12.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".
#include "napi/native_api.h"
#include "../include/WorkDemo.h"
#include "../include/ClassBind.h"
#include <hilog/log.h>
#define LOG_DOMAIN 0
#define LOG_TAG "demoTest"

#ifndef NAPI_learning_NapiRegister_H
#define NAPI_learning_NapiRegister_H
/*
 * 采用C接口进行暴露，使用C++形式暴露会导致so无法加载
 */

static napi_value aboutToAppear(napi_env env, napi_callback_info info)
{
    OH_LOG_INFO(LOG_APP, "aboutToAppear");
    int number = 10;
    napi_value sum;
    napi_create_int64(env, number, &sum);
    return sum;
}

static napi_value aboutToDisappear(napi_env env, napi_callback_info info)
{
    OH_LOG_INFO(LOG_APP, "aboutToDisappear");
    int number = -10;
    napi_value sum;
    napi_create_int64(env, number, &sum);
    return sum;
}

EXTERN_C_START
static napi_value getNativeInterface(napi_env env, napi_callback_info info)
{
    napi_value exports;
    size_t argc = 1;
    napi_value args[1] = {nullptr};
    napi_get_cb_info(env, info, &argc, args , nullptr, nullptr);

    napi_valuetype valuetype0;
    napi_typeof(env, args[0], &valuetype0);
    int64_t value;
    napi_get_value_int64(env, args[0], &value);
    
    napi_create_object(env, &exports);

    //if(value == 1){
    napi_property_descriptor desc[] = {
        { "aboutToAppear", nullptr, aboutToAppear, nullptr, nullptr, nullptr, napi_default, nullptr },
        { "aboutToDisappear", nullptr, aboutToDisappear, nullptr, nullptr, nullptr, napi_default, nullptr }
    };
    napi_define_properties(env, exports, sizeof(desc)/sizeof(desc[0]), desc);
    //}else{
        
    //}
    return exports;
}

static napi_value Init(napi_env env, napi_value exports)
{
    //定义暴露模块上的方法
    napi_property_descriptor desc[] = {
        { "add", nullptr, WorkDemo::Add, nullptr, nullptr, nullptr, napi_default, nullptr },
        { "sub", nullptr, WorkDemo::Sub, nullptr, nullptr, nullptr, napi_default, nullptr },
        { "hyPotC", nullptr, WorkDemo::hyPotC, nullptr, nullptr, nullptr, napi_default, nullptr },
        { "addSync", nullptr, WorkDemo::addSync, nullptr, nullptr, nullptr, napi_default, nullptr },
        { "addAsyncByPromise", nullptr, WorkDemo::addAsyncByPromise, nullptr, nullptr, nullptr, napi_default, nullptr },
        { "getAsyncByCallback", nullptr, WorkDemo::getAsyncByCallback, nullptr, nullptr, nullptr, napi_default, nullptr },
        
        //
        { "callPromiseMethod", nullptr, WorkDemo::CallPromiseMethod, nullptr, nullptr, nullptr, napi_default, nullptr },

        { "CallPromiseMethodSync", nullptr, WorkDemo::CallPromiseMethodSync, nullptr, nullptr, nullptr, napi_default, nullptr },

        { "getNativeInterface", nullptr, getNativeInterface, nullptr, nullptr, nullptr, napi_default, nullptr }

    };
    
    napi_property_descriptor classProp[] = {
        { "operateMethod", nullptr, ClassBind::JsOperate, nullptr, nullptr, nullptr, napi_default, nullptr}
    };
    
    napi_value classBind = nullptr;
    const char* classBindName = "classBind";
    napi_define_class(env, classBindName, sizeof(classBindName), ClassBind::JsConstructor, nullptr, 1, classProp, &classBind);
    napi_set_named_property(env, exports, "classBind", classBind);
    
    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    return exports;
}
EXTERN_C_END

static napi_module demoModule = {
    .nm_version =1,
    .nm_flags = 0,
    .nm_filename = nullptr,
    .nm_register_func = Init,   //模块注册调用方法，主要用来注册方法到引擎中
    .nm_modname = "entry",      //模块名称，一般编译出so名称相同
    .nm_priv = ((void*)0),
    .reserved = { 0 },
};

// 加载so的构造函数，一般用来注册so模块
extern "C" __attribute__((constructor)) void RegisterHelloModule(void)
{
    napi_module_register(&demoModule);
}

#endif //NAPI_learning_NapiRegister_H
